import {createStore} from 'vuex'
import {ColorStatus, MainWinPageStatus} from "../util/status"
import {ChangeNowChartNumber, ChangeNowChartUid, ChangeNowFriendUid, ChartStatus} from "./ChartStatus"
// import {WebSocketObject} from "./websocket"
import {MsgStatus} from "./static"

export const store = createStore({
    // 读取的时候直接调用,如果修改状态的话就调用下面的方法.
    state() {
        return {
            winStatu: 0,
            rightTitle: "我的Android手机",
            rightSignature: "昨天是段历史，明天是个谜团，而今天是天赐的礼物.",
            nowMessage: "",
            ackQueue: new Map(),
            leftStatus: {
                iconColorStatu: {
                    sessionIcon: "#3498db",
                    friendIcon: '#57606f',
                    cloudIcon: '#57606f',
                    addFreiend: '#57606f',
                },
                // 1 是展示当前会话. 2. 展示好友.   3. 展示工具(待定.).
                // 动态的.
                mainMenuStatu: 1,
                // 需要定义静态的状态标志,用于后续需要. 直接调.
                mainWinPageStatus: MainWinPageStatus,
            },
            rightStatus: {
                addFriendTypeStatus: 0, // 0好友 1群组.
            },
            userInfo: {
                id: "",
                token: "",
                img_url: "",
                username: "",
                seq: "",
                login_account_string: "",
            },
            addfriend: {
                result: {},
                isShow: false,
                img_url: "",
            },
            addgroup: {},
            chartStatus: ChartStatus,

            static: {
                msgStatic: MsgStatus
            },
            ws: "",
            test: "test",
        }
    },
    // 改变状态的方法,通过commit直接调用.
    mutations: {
        loginSuc(state, data) {
            // 跳转到主界面.
            state.winStatu = 1
            state.chartStatus.allFriends = data.friends
            // 赋值all groups.
            state.chartStatus.allGroup = data.groups
            // let initRightTop = data.sessions[0]
            // 很可能是这个initRightTop导致的问题哈.
            // TODO: 为啥呢？
            // state.chartStatus.nowSessionInfo = data.sessions[0]
            state.chartStatus.nowSessions = data.sessions
            if (data.sessions === null) {
                data.sessions = []
            } else {
                if (data.sessions.length > 0) {
                    // if (data.sessions[0].length > 0) {
                    // 初始化now session info.
                    state.rightTitle = data.sessions[0].user_name
                    state.rightSignature = data.sessions[0].signature
                    // 初始化.
                    state.chartStatus.nowSessionInfo.uid = data.sessions[0].uid
                    state.chartStatus.nowSessionInfo.user_name = data.sessions[0].user_name
                    state.chartStatus.nowSessionInfo.user_img_url = data.sessions[0].user_img_url
                    state.chartStatus.nowSessionInfo.signature = data.sessions[0].signature
                    // }
                }
            }

            // 初始化 userInfo
            state.userInfo = data.user
            // 登录成功之后,for循环 设置所有arr_message为null的情况.
            for (let i = 0; i < state.chartStatus.nowSessions.length; i++) {
                if (state.chartStatus.nowSessions[i].array_message === null) {
                    state.chartStatus.nowSessions[i].array_message = []
                }
            }
        },

        loginFail(state) {
            state.winStatu = 2
        },
        clearMessage(state) {
            state.nowMessage = ""
        },
        setWinState(state, num) {
            state.winStatu = num
        },
        setUserToken(state, token) {
            state.userInfo.token = token
        },
        setAddFriendResult(state, data) {
            state.addfriend.result = data
            state.addfriend.isShow = true
        },
        setAddFriend(state, data) {
            // 更新好友.
            state.chartStatus.allFriends.push(data.NewFriend)
            // 更新session,将当前好友设置第一个session.
            state.chartStatus.nowSessions.unshift(data.NewSession)
        },
        setNowSessionInfos(state, session) {
            state.chartStatus.nowSessionInfo.uid = session.uid
            state.chartStatus.nowSessionInfo.user_name = session.user_name
            state.chartStatus.nowSessionInfo.user_img_url = session.user_img_url
            state.chartStatus.nowChartUid = session.uid
        },
        // setNowSessionToTop(state, idx) {
        //     // let d = state.chartStatus.nowSessions.slice(idx, idx + 1)
        //     // TODO: 当前深浅拷贝问题.
        //     // let d = Object.create(state.chartStatus.nowSessions[idx])
        //     // state.chartStatus.nowSessions.unshift(d)
        //     // state.chartStatus.nowSessions[idx + 1] = {}
        // },

        setAddMessage(state, message) {
            // 只需要将message挂载到某个session中
            // 这里必须通过for循环来进行遍历.
            // state.chartStatus.nowSessions[state.chartStatus.nowChartNumber].array_message.push(message)
            // 主动给我发送消息的人.
            // 这里还有问题: 现在需要确定的是你这个消息属于按个session.
            state.chartStatus.nowSessions.filter((value, index) => {
                // 谁的tid===session中的uid(也就是这个朋友的ID.).
                if (value.id === message.sid) {
                    // 赋值.
                    value.array_message.push(message)
                    // last_message .
                    // 如果当前会话没有选中,则置黄.
                    if (index !== state.chartStatus.nowChartNumber) {
                        value.have_no_read_message = 1
                    }
                    value.last_message = message.body
                    if (message.type === 6){
                        // 群消息.
                        value.last_message = message.username+":"+message.body
                    }
                }
            })
        },
        userLoginNotify(state, obj) {
            // 如何去做呢?
            // 用户好友上线的提醒通知,点亮朋友头像.
            state.chartStatus.nowSessions.filter((value) => {
                if (value.id === obj.sid) {
                    // 当前会话
                    value.is_online = '1'
                }
            })
        },
        friendOffLineNotify(state, obj) {
            // 还是找到当前session.
            state.chartStatus.nowSessions.filter((value) => {
                if (value.id === obj.sid) {
                    // 头像置灰.
                    value.is_online = '0'
                }
            })
        },
        setLeftStatusIconColorStatus(state, num) {
            state.leftStatus.mainMenuStatu = num
            state.rightSignature = "昨天是段历史，明天是个谜团，而今天是天赐的礼物。"
            state.rightStatus.addFriendTypeStatus = 0
            if (num === 1) {
                // 展示当前会话.
                state.leftStatus.iconColorStatu.sessionIcon = ColorStatus.blue
                state.leftStatus.iconColorStatu.friendIcon = ColorStatus.gray
                state.leftStatus.iconColorStatu.cloudIcon = ColorStatus.gray
                state.leftStatus.iconColorStatu.addFreiend = ColorStatus.gray
            } else if (num === 2) {
                state.leftStatus.iconColorStatu.friendIcon = ColorStatus.blue
                state.leftStatus.iconColorStatu.cloudIcon = ColorStatus.gray
                state.leftStatus.iconColorStatu.sessionIcon = ColorStatus.gray
                state.leftStatus.iconColorStatu.addFreiend = ColorStatus.gray
            } else if (num === 3) {
                state.leftStatus.iconColorStatu.cloudIcon = ColorStatus.blue
                state.leftStatus.iconColorStatu.sessionIcon = ColorStatus.gray
                state.leftStatus.iconColorStatu.friendIcon = ColorStatus.gray
                state.leftStatus.iconColorStatu.addFreiend = ColorStatus.gray
            } else if (num === 4) {
                state.leftStatus.iconColorStatu.addFreiend = ColorStatus.blue
                state.leftStatus.iconColorStatu.cloudIcon = ColorStatus.gray
                state.leftStatus.iconColorStatu.sessionIcon = ColorStatus.gray
                state.leftStatus.iconColorStatu.friendIcon = ColorStatus.gray
                // 顺便应该修改下名字.
                state.rightTitle = "添加新的朋友"
                state.rightSignature = "有朋自远方来,不亦乐乎."
            }
        },
        setRightStatusAddFriendType(state, num) {
            state.rightStatus.addFriendTypeStatus = num
            // 清空数据.
            state.addfriend = {}
            state.addgroup = {}
        },
        ChangeNowChartUid,
        ChangeNowChartNumber,
        ChangeNowFriendUid,
    }
})